#!/usr/bin/python
"""Title 24 Local Ordinance compliance calculator."""


import datetime
import tempfile
#Import needed adjustment from modules.  The idea is that the different ordinances will use the
#same type of calculation, just use different adjustment factors that will be stored outside of the main_module
#for easy editing.
import marin
import dat_reader
from printing.settings import SHORT_DATE_FORMAT
from printing.pdf import render


def calc_compliance(calculator):
    """Prints out the results of the calculation to the user."""
    print 'Standard: ', calculator.revised_std
    print 'Proposed: ', calculator.prop
    print calculator.adjustment_factor
    if not calculator.complies:
        print 'Your house does not comply.'
    else:
        print 'Your house complies.'


def user_select():
    """Will prompt the user to select whether or not to use the automatic
    .dat reader or to input the details manually and eventually select
    what Ordinance to run.
    """
    print 'Welcome to the Local Ordinance Calculator'
    print "\n"
    is_solar = int(raw_input(
        "Will you be installing solar panels? \n" \
        "Enter (1) for yes, (2) for no:\n"
    ))
    is_dat = int(raw_input(
        "To enter data manually, enter (1). \n" \
        "To use EnergyPro's .dat file, enter (2):\n"
    ))

    return(is_solar, is_dat)


def user_input():
    """Collect the user input and return a LocalOrdinanceCalculator object."""
    return dat_reader.LocalOrdinanceCalculator(
        proj_name=raw_input('What is the name of your project?: '),
        cz=int(raw_input('Input in your climate zone: ')),
        std=float(raw_input('Input your Standard Total: ')),
        prop=float(raw_input('Input your Proposed Total: ')),
        area=float(raw_input('Input your total area in square feet: ')),
        local_ordinance_module=marin,
    )


def make_pdf(calculator, target_dir=None):
    """Creates a PDF report from the data stored in the calculator and
    returns the path to the new pdf file.
    """
    if not target_dir: target_dir = tempfile.gettempdir()
    return render(
        render_dir=target_dir,
        filename='mcres_%s.pdf' \
            % datetime.datetime.now().strftime('%Y%m%d%H%M%s'),
        project_title=calculator.proj_name,
        project_addr_formatted='%s\n%s, %s %s' % (
            calculator.proj_addr,
            calculator.proj_city,
            calculator.proj_state,
            calculator.proj_zipcode,
        ),
        project_cfa=calculator.area,
        doc_author_company_name=calculator.doc_business,
        doc_author_name=calculator.doc_author,
        doc_author_addr_formatted='%s\n%s, %s %s' % (
            calculator.doc_addr,
            calculator.doc_city,
            calculator.doc_state,
            calculator.doc_zipcode,
        ),
        # FIXME: Collect Designer info
        designer_company_name='',
        designer_name='',
        designer_addr_formatted='',
        footer_left_text='',
        footer_center_text=calculator.doc_business,
        footer_right_text='',
        date_str=datetime.datetime.now().strftime(SHORT_DATE_FORMAT),
        standard_t24=calculator.std,
        proposed_t24=calculator.prop,
        adjustment_factor_standard=calculator.adjustment_factor,
        adjustment_factor_proposed=calculator.adjustment_factor_proposed,
        adjusted_standard_design= "%0.2f" % calculator.revised_std,
        max_allowed_proposed= "%0.2f" % calculator.max_allowed_proposed,
        annual_pv_production=calculator.pv_production,
        solar_pv_credit="%0.2f" % calculator.solar_pv_credit,
        adjusted_proposed_design="%0.2f" % calculator.adjusted_prop,
    )


def master_mind():
    """Main function for interacting with the user from the command line."""
    # calc will be an instance of the dat_reader.LocalOrdinanceCalculator class
    calc = None
    (is_solar, is_dat) = user_select()

    if is_dat == 1:
        calc = user_input()
    elif is_dat == 2:
        calc = dat_reader.dat_read()
        calc.local_ordinance_module = marin
        if calc.adjustment_factor == 1:
            print 'Your climate zone or floor area do not meet the ' \
                  'minimum requirements'
    else:
        print 'You have entered an invalid number'

    if is_solar == 1:
        calc.pv_production = float(raw_input("Input Annual PV Production: "))
    else:
        calc.pv_production = 0

    calc_compliance(calc)

    print "PDF rendered to: '%s'" % make_pdf(calculator=calc)


if __name__ == "__main__":
    master_mind()
